<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>提交一个事务</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.begintransaction.html">PDO::beginTransaction</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.construct.html">PDO::__construct</a></div>
 <div class="up"><a href="class.pdo.html">PDO</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="pdo.commit" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::commit</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::commit</span> &mdash; <span class="dc-title">
   提交一个事务
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pdo.commit-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">bool</span> <span class="methodname"><strong>PDO::commit</strong></span>
    ( <span class="methodparam">void</span>
   )</div>

  <p class="para rdfs-comment">
  提交一个事务，数据库连接返回到自动提交模式直到下次调用 <span class="function"><a href="pdo.begintransaction.html" class="function">PDO::beginTransaction()</a></span> 开始一个新的事务为止。
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.commit-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code>TRUE</code></strong>， 或者在失败时返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.commit-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-890">
    <p><strong>Example #1 提交一个基础事务</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;开始一个事务，关闭自动提交&nbsp;*/<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">beginTransaction</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;在全有或全无的基础上插入多行记录（要么全部插入，要么全部不插入）&nbsp;*/<br /></span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'INSERT&nbsp;INTO&nbsp;fruit<br />&nbsp;&nbsp;&nbsp;&nbsp;(name,&nbsp;colour,&nbsp;calories)<br />&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(?,&nbsp;?,&nbsp;?)'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$fruits&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">colour</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$fruit</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">calories</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;));<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;提交更改&nbsp;*/<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">commit</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;现在数据库连接返回到自动提交模式&nbsp;*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-891">
    <p><strong>Example #2 提交一个DDL事务</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;&nbsp;开始一个事务，关闭自动提交&nbsp;*/<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">beginTransaction</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;Change&nbsp;the&nbsp;database&nbsp;schema&nbsp;*/<br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"DROP&nbsp;TABLE&nbsp;fruit"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;更改数据库架构&nbsp;*/<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">commit</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;现在数据库连接返回到自动提交模式&nbsp;*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
     并不是所有数据库都允许使用DDL语句进行事务操作：有些会产生错误，而其他一些（包括MySQL）会在遇到第一个DDL语句后就自动提交事务。
    </span>
   </p></blockquote>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdo.commit-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdo.begintransaction.html" class="function" rel="rdfs-seeAlso">PDO::beginTransaction()</a> - 启动一个事务</span></li>
    <li class="member"><span class="function"><a href="pdo.rollback.html" class="function" rel="rdfs-seeAlso">PDO::rollBack()</a> - 回滚一个事务</span></li>
    <li class="member"><a href="pdo.transactions.html" class="link">事务和自动提交</a></li>
   </ul>
  </p>
 </div>



</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdo.begintransaction.html">PDO::beginTransaction</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdo.construct.html">PDO::__construct</a></div>
 <div class="up"><a href="class.pdo.html">PDO</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
